package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.service.UserFeign;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.user.User;
import com.qingcheng.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {
    @Autowired
    private UserFeign userServiceFeign;

    @RequestMapping("/loginUI")
    public String loginUI(){
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody User user) {
        Result result = userServiceFeign.loginUser(user);
        return result;
    }

    @GetMapping("/getUserInfo")
    @ResponseBody
    public Result getUserInfo(HttpServletRequest request) {
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String username = null;
        try {
            username = JwtUtils.getMemberIdByJwtToken(request);
        } catch (Exception e) {
            username = "Patrick";
        }
        Result result = userServiceFeign.getUserInfo(username);
        return result;
    }
}
